#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os, sys

sys.path.append("../code")

import pandas as pd
from database import con, droptable,inster
from tqdm import tqdm
# ETF
# 读取base并保存到数据库
etfpath = os.path.join(os.path.expanduser("~"), "stockdate/cvs",
                       "etf_basic.csv")
df = pd.read_csv(etfpath)
df = df.rename(columns={'symbol': 'code'})
droptable("etfbasic")
df = df[["code", "name"]]
df["changeTime"] = None
df.to_sql('etfbasic', con=con, index=False, index_label="code")

# 遍历 etfdaiky 保存到 sql


pbar = tqdm(total=len(df.index), desc="cache read:ETF日线数据")

for index, row in df.iterrows():
    pbar.set_description("cache read:ETF日线数据"+str(row["code"]+ row["name"]))
    pbar.update(1)
    path =  os.path.join(os.path.expanduser("~"), "stockdate/cvs/etf", row["code"] + '.csv')
    if os.path.exists(path) == False:
        continue
    tdf = pd.read_csv(path)
    tdf["code"] = row["code"]
    tdf = tdf.rename(columns={'volume': 'vol'})
    tdf['date'] = tdf['date'].astype('datetime64[ns]').dt.strftime('%Y%m%d')
    inster("etfdaily",columns=["date","code","open","close","high","low","vol"],df=tdf)
   
pbar.close()

   
